home *** CD-ROM | disk | FTP | other *** search
/ Singles Flirt Up Your Life! (German) / Singles Flirt Up Your Life.iso / data1.cab / Statemachine / tv.lua < prev    next >
Text File  |  2004-01-29  |  9KB  |  331 lines

  1. -- tv state machine
  2.  
  3. beginStateMachine()
  4.  
  5.     onEnter(function(msg)
  6.         setState("Off");
  7.     end )
  8.     
  9.     
  10.     onMsg("buildMenu", function(msg)
  11.     
  12.         if (repairMenu()) then return end
  13.     
  14.         local character = getStateObjectFromID(msg.sender);
  15.         
  16.         
  17.  
  18.         -- build the pie menu
  19.         clearPieMenu();
  20.         local button ;
  21.         
  22.         local isWatching = isUser(character, this, TV_ACTIVITIES);
  23.  
  24.         
  25.         if (not isUser(character, this, {"watchRomantic"})) then
  26.             button = addPieMenuButton("pm_watchRomantic", "watch", "Romantic");
  27.             button.addDescription(ACTIVITY, "watchRomantic");
  28.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  29.         end
  30.         
  31.         if (not isUser(character, this, {"watchNews"})) then
  32.             button = addPieMenuButton("pm_watchNews", "watch", "News");
  33.             button.addDescription(ACTIVITY, "watchNews");
  34.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  35.         end
  36.         
  37.         if (not isUser(character, this, {"watchAction"})) then
  38.             button = addPieMenuButton("pm_watchAction", "watch", "Action");
  39.             button.addDescription(ACTIVITY, "watchAction");
  40.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  41.         end
  42.         
  43.         if (not isUser(character, this, {"watchSitcom"})) then
  44.             button = addPieMenuButton("pm_watchSitcom", "watch", "Sitcom");
  45.             button.addDescription(ACTIVITY, "watchSitcom");
  46.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  47.         end
  48.         
  49.         if (not isUser(character, this, {"watchHorror"})) then
  50.             button = addPieMenuButton("pm_watchHorror", "watch", "Horror");
  51.             button.addDescription(ACTIVITY, "watchHorror");
  52.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  53.         end
  54.  
  55.         if (this.getState() ~= "Off") then
  56.         
  57.             print("tv.getState() " .. this.getState());
  58.             button = addPieMenuButton("pm_switchOff", "off");
  59.             button.addDescription(DONTQUEUE, "true");
  60.             
  61.             if (not isWatching) then
  62.                 button = addPieMenuButton("pm_watchJoin", "watch", this.getState());
  63.             end
  64.         end
  65.     end )
  66.     
  67.         -- switch on tv
  68.     onMsg("off", function(msg)    
  69.         setState("Off");
  70.     end )
  71.     
  72.     
  73.     -- switch on tv
  74.     onMsg("watch", function(msg)
  75.     
  76.         print(" TV onMsg watch data:" .. msg.data .. "  sender:" .. msg.sender);    
  77.     
  78.         -- get character who initiated this action
  79.         local character = getStateObjectFromID(msg.sender);
  80.         -- if this is broken: abort characters action
  81.         if abortIfBroken(character) then return end;        
  82.         local wso = character.walkSO;
  83.         
  84.         if (isUser(character, this, TV_ACTIVITIES)) then
  85.             --character allready watching this TV:  send switch msg
  86.             print(" TV onMsg watch is User" .. wso.getStateMachine().getName() .. "." .. wso.getState());    
  87.             sendMsg("switch", wso, msg.data);
  88.         else
  89.             --character not watching this TV:  find seat to watch
  90.             print(" TV onMsg watch is not User" .. wso.getStateMachine().getName() .. "." .. getState());    
  91.             local seat, actionPoint = getNextWatchSit(character, this, 1.0, 1.5);
  92.             if (not seat) then
  93.                 print("no seat to watch tv found");
  94.                 instantAbort(character, EMOTICON_NOSOFA, "emoThink")
  95.                 return
  96.             end
  97.             
  98.             if (wso.walkToActionPoint(actionPoint)) then
  99.             
  100.                 if (seat.hasBehavior("sofa") or seat.hasBehavior("armchair")) then
  101.                 
  102.                     -- create state machine contexts
  103.                     local wsoContext = StateMachineContext();
  104.                     --wsoContext.storeStateObject("seat", seat);
  105.                     wsoContext.storeData("actionPointName", actionPoint.getName());
  106.                     wsoContext.storeData("sitState", "watchTV");
  107.                     wsoContext.storeStateObject("tv", this);
  108.                     wsoContext.storeData("channel", msg.data);
  109.                     --wso.queueStateMachine("sofaChar.sitDownToWatchTV", this, wsoContext);                
  110.                     wso.queueStateMachine("sofaChar.sitDown", seat, wsoContext);                
  111.                 else
  112.                     print("unkown seat type to watch tv");
  113.                     instantAbort(character, EMOTICON_CANNOT, "emoThink")
  114.                 end
  115.             else
  116.                 print("no path found");
  117.                 instantAbort(character, EMOTICON_NOPATH, "emoThink")
  118.             end
  119.         end
  120.         
  121.     end )
  122.     
  123.         
  124.     -- switch on tv when allready sitting in front of it
  125.     onMsg("watchShortcut", function(msg)
  126.     
  127.         print(" TV onMsg watchShortcut data:" .. msg.data .. "  sender:" .. msg.sender);    
  128.         local character = getStateObjectFromID(msg.sender);
  129.         local wso = character.walkSO;
  130.         
  131.         wso.storeStateObject("tv", this);
  132.         wso.storeData("channel", msg.data);
  133.         
  134.     end )
  135.         
  136.         
  137.     -- repair
  138.     onMsg("repair", function(msg)
  139.     
  140.         print("onMsg repair");
  141.         -- get character who initiated this action
  142.         local character = getStateObjectFromID(msg.sender);
  143.         -- walk to the closest action point
  144.         local actionPoint = character.getFreeActionPoint(this, "repair");
  145.         -- get the walk state object
  146.         local wso = character.walkSO;
  147.         if (actionPoint) then
  148.             -- create state machine contexts
  149.             local wsoContext = StateMachineContext();
  150.             -- store the action point
  151.             wsoContext.storeData("actionPointName", actionPoint.getName());
  152.             if (wso.walkToActionPoint(actionPoint)) then
  153.                 wso.queueStateMachine("repairChar.repairStart", this, wsoContext);
  154.             else
  155.                 print("no path found");
  156.                 instantAbort(character, EMOTICON_NOPATH, "emoThink")
  157.             end
  158.         else
  159.             print("no action point found");
  160.             instantAbort(character, EMOTICON_CANNOT, "emoThink")
  161.         end
  162.     end )
  163.     
  164.     
  165.     -- switch
  166.     onMsg("switch", function(msg)
  167.     
  168.         print("TV: onMsg switch" .. msg.data);
  169.         
  170.         setState(msg.data);
  171.         
  172.     end )
  173.         
  174.     -- a chracter stoped watching this tv
  175.     onMsg("stopWatch", function(msg)
  176.         local users = getCharactersWithActivities(this, TV_ACTIVITIES);
  177.         if (getn(users) < 1) then
  178.             setState("Off");
  179.         end
  180.     end )
  181.         
  182.         
  183.     state("Off")
  184.     
  185.         onEnter(function(msg)
  186.             print("Off enter ----------------------------");
  187.             local users = getCharactersWithActivities(this, TV_ACTIVITIES);
  188.             for u = 1, getn(users) do
  189.                 local user = users[u];
  190.                 sendMsg("tvOff", user.walkSO);
  191.             end
  192.             
  193.             replaceTexture("tvOff", "tvOff");
  194.             freePreloadedTextures();
  195.             playSound("tvOff");
  196.         end )
  197.     
  198.         
  199.         onExit(function(msg)
  200.             playSound("tvOn");
  201.         end )        
  202.         
  203.     state("Romantic")
  204.     
  205.         onEnter(function(msg)
  206.             print("Romantic enter ----------------------------");
  207.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Romantic")
  208.             sendMsgThis("startAnim");
  209.             
  210.             loopSound("tvRomantic");
  211.             preloadTextures(this, {"tvRomantic1", "tvRomantic2", "tvRomantic3", "tvRomantic4"});
  212.         end )
  213.         
  214.         
  215.         onMsg("startAnim", function(msg)
  216.             startAnim({"tvRomantic1", "tvRomantic2", "tvRomantic3", "tvRomantic4", "tvRomantic3", "tvRomantic2"});
  217.         end )
  218.     
  219.         onMsg("showPic", function(msg)
  220.             replaceTexture("tvOff", msg.data);
  221.         end )
  222.         
  223.         onExit(function(msg)
  224.             stopSound("tvRomantic");
  225.         end )        
  226.                     
  227.     state("Sitcom")
  228.     
  229.         onEnter(function(msg)
  230.             print("Sitcom enter ----------------------------");
  231.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Sitcom")
  232.             sendMsgThis("startAnim");
  233.             
  234.             loopSound("tvSitcom");
  235.             preloadTextures(this, {"tvSitcom1", "tvSitcom2", "tvSitcom3", "tvSitcom4"});
  236.         end )
  237.         
  238.         
  239.         onMsg("startAnim", function(msg)
  240.             startAnim({"tvSitcom1", "tvSitcom2", "tvSitcom3", "tvSitcom4", "tvSitcom3", "tvSitcom2"});
  241.         end )
  242.     
  243.         onMsg("showPic", function(msg)
  244.             replaceTexture("tvOff", msg.data);
  245.         end )
  246.     
  247.         
  248.         onExit(function(msg)
  249.             stopSound("tvSitcom");
  250.         end )        
  251.                     
  252.     state("Action")
  253.     
  254.         onEnter(function(msg)
  255.             print("Action enter ----------------------------");
  256.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Action")
  257.             sendMsgThis("startAnim");
  258.             
  259.             loopSound("tvAction");
  260.             preloadTextures(this, {"tvAction1", "tvAction2", "tvAction3", "tvAction4"});
  261.         end )
  262.         
  263.         
  264.         onMsg("startAnim", function(msg)
  265.             startAnim({"tvAction1", "tvAction2", "tvAction3", "tvAction4"});
  266.         end )
  267.     
  268.         onMsg("showPic", function(msg)
  269.             replaceTexture("tvOff", msg.data);
  270.         end )
  271.     
  272.         
  273.         onExit(function(msg)
  274.             stopSound("tvAction");
  275.         end )        
  276.         
  277.     state("News")
  278.     
  279.         onEnter(function(msg)
  280.             print("News enter ----------------------------");
  281.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "News")
  282.             sendMsgThis("startAnim");
  283.             
  284.             loopSound("tvNews");
  285.             preloadTextures(this, {"tvNews1", "tvNews2", "tvNews3", "tvNews4"});
  286.         end )
  287.         
  288.         
  289.         onMsg("startAnim", function(msg)
  290.             startAnim({"tvNews1", "tvNews2"});
  291.         end )
  292.     
  293.         onMsg("showPic", function(msg)
  294.             replaceTexture("tvOff", msg.data);
  295.         end )
  296.     
  297.         
  298.         onExit(function(msg)
  299.             stopSound("tvNews");
  300.         end )
  301.         
  302.     state("Horror")
  303.     
  304.         onEnter(function(msg)
  305.             print("Horror enter ----------------------------");
  306.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Horror")
  307.             sendMsgThis("startAnim");
  308.             
  309.             loopSound("tvHorror");
  310.             preloadTextures(this, {"tvHorror1", "tvHorror2", "tvHorror3", "tvHorror4"});
  311.         end )
  312.         
  313.         
  314.         onMsg("startAnim", function(msg)
  315.             startAnim({"tvHorror1", "tvHorror2", "tvHorror3", "tvHorror4", "tvHorror3", "tvHorror2"});
  316.         end )
  317.     
  318.         onMsg("showPic", function(msg)
  319.             replaceTexture("tvOff", msg.data);
  320.         end )
  321.     
  322.         
  323.         onExit(function(msg)
  324.             stopSound("tvHorror");
  325.         end )        
  326.         
  327.         
  328.                 
  329.             
  330. endStateMachine()
  331.